﻿using System;
using System.IO;
using System.Linq;

namespace ServerCrafter.ClassLibrary
{
    public static class ModManager
    {
        public static class InstalledMods
        {
            public static string[] List;
            public static int Count;

            public static string[] CreateList()
            {
                Logger.WriteInfo("Installed mods list requested. Getting list.");
                Logger.WriteInfo("Looking in directory " + Settings.ServerStartInfo.WorkingDirectory + @"mods\" + ".");
                try
                {
                    List = Directory.GetFiles(Settings.ServerStartInfo.WorkingDirectory + @"mods\", ".")
                                    .Where(file => file.ToLower().EndsWith(".jar") || file.ToLower().EndsWith(".zip"))
                                    .ToArray();
                }
                catch (ArgumentException exception)
                {
                    Logger.WriteWarning(
                        "Could not get installed mods list! Path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.");
                    Logger.WriteInfo("\tException: " + exception);
                }
                catch (PathTooLongException exception)
                {
                    Logger.WriteWarning(
                        "Could not get installed mods list! The specified path, file name, or both exceed the system-defined maximum length.");
                    Logger.WriteWarning("\tException: " + exception);
                }
                catch (DirectoryNotFoundException exception)
                {
                    Logger.WriteWarning("The specified path is invalid (for example, it is on an unmapped drive).");
                    Logger.WriteWarning("\tException: " + exception);
                }
                catch (IOException exception)
                {
                    Logger.WriteWarning("Could not get installed mods list! Path is a file name.");
                    Logger.WriteWarning("\tException: " + exception);
                }

                var index = 0;
                foreach (var mod in List)
                {
                    List[index] = Path.GetFileNameWithoutExtension(mod);
                    index++;
                }
                Count = List.Count();

                return List;
            }
        }

        public static class InstalledCoreMods
        {
            public static string[] List;
            public static int Count;

            public static string[] CreateList()
            {
                Logger.WriteInfo("Installed core mods list requested. Getting list.");
                Logger.WriteInfo("Looking in directory " + Settings.ServerStartInfo.WorkingDirectory + @"coremods\" + ".");
                try
                {
                    List = Directory.GetFiles(Settings.ServerStartInfo.WorkingDirectory + @"coremods\", ".")
                                    .Where(file => file.ToLower().EndsWith(".jar") || file.ToLower().EndsWith(".zip"))
                                    .ToArray();
                }
                catch (ArgumentException exception)
                {
                    Logger.WriteWarning(
                        "Could not get installed mods list! Path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.");
                    Logger.WriteInfo("\tException: " + exception);
                }
                catch (PathTooLongException exception)
                {
                    Logger.WriteWarning(
                        "Could not get installed mods list! The specified path, file name, or both exceed the system-defined maximum length.");
                    Logger.WriteWarning("\tException: " + exception);
                }
                catch (DirectoryNotFoundException exception)
                {
                    Logger.WriteWarning("The specified path is invalid (for example, it is on an unmapped drive).");
                    Logger.WriteWarning("\tException: " + exception);
                }
                catch (IOException exception)
                {
                    Logger.WriteWarning("Could not get installed mods list! Path is a file name.");
                    Logger.WriteWarning("\tException: " + exception);
                }

                var index = 0;
                foreach (var coremod in List)
                {
                    List[index] = Path.GetFileNameWithoutExtension(coremod);
                    index++;
                }
                Count = List.Count();

                return List;
            }
        }
    }
}